package com.jn.erp.wechat.staff.api.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.common.utils.StringUtils;
import com.jn.erp.security.entity.SysRole;
import com.jn.erp.security.entity.SysUserRole;
import com.jn.erp.security.sysservice.SysUserRoleService;
import com.jn.erp.wechat.common.api.controller.base.BaseWxAuthController;
import com.jn.erp.wechat.common.api.entity.WxMenu;
import com.jn.erp.wechat.common.api.entity.dto.WxUserDto;
import com.jn.erp.wechat.common.api.service.WxMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <pre>
 *
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/9/10 19:52
 */
@RestController
@RequestMapping("/wx/menu")
public class MpMenuController extends BaseWxAuthController {

    @Autowired
    private WxMenuService wxMenuService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    //获取用户信息
    @PostMapping("/getTreeByRouteName")
    @Transactional
    public AjaxResult getTreeByRouteName(@RequestBody WxMenu wxMenu) {
        WxUserDto wxUser=getWxUser();
        List<SysUserRole> userRoles = sysUserRoleService.getRolesByUserOrgId(wxUser.getUserId(), wxUser.getOrgIdLong());
        List<WxMenu> wxMenus =new ArrayList<>();
        //未登录用户，展示全部内容
        if (userRoles.size() == 0) {
//            return error("该用户为分配角色");
            QueryWrapper<WxMenu> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("menu_type","E");
            queryWrapper.eq("status","0");
            wxMenus=wxMenuService.list(queryWrapper);
        }else{
            List<Long> collect = userRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
            String roles = StringUtils.join(collect, ",");
            wxMenu.setRoles(roles);
            wxMenus = wxMenuService.wxUserRoleMenus(wxMenu);
        }

        List<WxMenu> childPerms = wxMenuService.getChildPerms(wxMenus, 0l);
        Stream<WxMenu> wxMenuStream = childPerms.stream().filter(m -> StrUtil.equals(m.getRouteName(), wxMenu.getRouteName()));
        List<WxMenu> collect1 = wxMenuStream.collect(Collectors.toList());
        if(collect1.isEmpty()){
            return error("未正确配置用户移动端角色");
        }
        List<WxMenu> child=collect1.get(0).getChildren();
        return success(child);
    }



//    //移动端获取展示树
//    @PostMapping("/getTeacherMenu")
//    public AjaxResult getTeacherMenu(){
//        List<SysRole> roleIds = getLoginUser().getUser().getRoles();
//        if (roleIds.size() == 0) {
//            return error("该用户未分配角色");
//        }
//        List<Long> collect = roleIds.stream().map(SysRole::getRoleId).collect(Collectors.toList());
//        String roles = StringUtils.join(collect, ",");
//        WxMenu wxMenu=new WxMenu();
//        wxMenu.setRoles(roles);
//        List<WxMenu> wxMenus = wxMenuService.wxUserRoleMenus(wxMenu);
//        List<WxMenu> childPerms = wxMenuService.getChildPerms(wxMenus, 0l);
//        return success(childPerms);
//    }



}
